var helangSearch = {
    els: {},
    searchIndex: 0,
    hot: {
        id: [],
        color: ['#ff2c00', '#ff5a00', '#ff8105', '#fd9a15', '#dfad1c'],
        list: [],
    },
    init: function () {
        var _this = this;
        this.els = {
            pickerBtn: $(".picker"),
            pickerList: $(".picker-list"),
            logo: $(".logo"),
            hotList: $(".hot-list"),
            input: $("#search-input"),
            logOut: $("#log_out"),
            login: $("#login"),
            user_id: $("#user_id"),
            user_name: $("#user_name"),
            avatar: $('.hr_avator'),
        };
        this.els.hotList.html(function () {
            var str = '';
            var types = 0;
            if ($('.picker').text().indexOf("图片") != -1)
                types = 1;
            $.each(_this.hot.list, function (index, item) {
                var content = item.split(' ');
                str += '<a href="/search/?types=' + types + '&content=' + content[0] + '" target="_blank">'
                    + '<div class="number" style="color: ' + _this.hot.color[index] + '">' + (index + 1) + '</div>'
                    + '<div>' + item + '</div>'
                    + '</a>';
            });
            return str;
        });
        this.els.pickerBtn.click(function () {
            if (_this.els.pickerList.is(':hidden')) {
                setTimeout(function () {
                    _this.els.pickerList.show();
                }, 100);
            }
        });
        this.els.pickerList.on("click", ">li", function () {
            _this.searchIndex = $(this).index();
            _this.els.pickerBtn.html($(this).html())
        });
        this.els.input.click(function () {
            if (!$(this).val()) {
                setTimeout(function () {
                    _this.els.hotList.show();
                }, 100);
            }
        });
        this.els.input.on("input", function () {
            if ($(this).val()) {
                _this.els.hotList.hide();
            }
        });
        $(document).click(function () {
            _this.els.pickerList.hide();
            _this.els.hotList.hide();
        });
        this.els.logOut.click(function () {
            $.get('/user/log_out/',function (data,status) {
                if (data == '1') {
                    alert('注销成功');
                    _this.els.user_id.text(-1);
                    _this.els.user_name.text('用户名');
                    _this.els.avatar.css("background-image","url(/static/images/m2/avator.png)");
                    $('#login').css('display','');
                    $('#user_single').css('display','none');
                    $('#log_out').css('display','none');
                }
                else alert('注销失败');
            })
        });
        this.els.login.click(function () {
            var url = window.location.href;
            window.location.href = '/user/login/?url=' + url;
        });
        $.get('/user/session_get/', function (data, status) {
            var jsonObj = JSON.parse(data);
            var code = jsonObj['status'];
            if (code == '200'){
                _this.els.user_id.text(jsonObj['info']['id']);
                _this.els.user_name.text(jsonObj['info']['name']);
                _this.els.avatar.css("background-image","url("+ jsonObj['info']['avatar'] +")");
                $('#login').css('display','none');
                $('#user_single').attr('href','/user/center/')
            }
            else {
                $('#user_single').css('display','none');
                $('#log_out').css('display','none');
            }
        });
    }

};
$(function () {
    helangSearch.init();
});
